package com.sky.service.impl;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //拷贝数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //封装userId
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //根据userId，dishId，setmealId,dishFlavor确定购物车内是否存在该订单
        ShoppingCart dbShoppingCart = shoppingCartMapper.getShoppingCart(shoppingCart);
        //如果dbShoppingCart不为空，则商品已经存在，则更新数量
        if (dbShoppingCart != null){
            //数量加1
            dbShoppingCart.setNumber(dbShoppingCart.getNumber() + 1);
            //更新数据
            shoppingCartMapper.updateShoppingCart(dbShoppingCart);
            System.out.println("新增成功");
        } else {
        //如果dbShoppingCart为null，则商品不存在，进行插入操作
            if (shoppingCart.getDishId() != null){
                //如果DishId,说明是菜品，则对菜品表进行查询
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                //补充菜品数据
                shoppingCart.setName(dish.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
                //插入数据
                shoppingCartMapper.save(shoppingCart);
                System.out.println("保存成功");
            }
            //如果SetmealId,说明是菜品，则对菜品表进行查询
            if (shoppingCart.getSetmealId() != null){
                //如果SetmealId,说明是菜品，则对菜品表进行查询
                Setmeal setmeal = setmealMapper.seleteById(shoppingCart.getSetmealId());
                //补充套餐数据
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
                //保存数据
                shoppingCartMapper.save(shoppingCart);
                System.out.println("保存成功");
            }
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        List<ShoppingCart> list = shoppingCartMapper.list(BaseContext.getCurrentId());
        return list;
    }

    /**
     * 删除购物车商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        // 复制数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 查询操作的商品
        shoppingCart = shoppingCartMapper.getShoppingCart(shoppingCart);
        // 判断数量，如果数量大于1，则商品减一，如果等于 1，则清空购物车
        if (shoppingCart.getNumber() > 1){
            //修改商品数量
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateShoppingCart(shoppingCart);
        } else {
            //根据id删除商品
            List<Long> ids = new ArrayList<>();
            ids.add(shoppingCart.getId());
            shoppingCartMapper.delete(ids);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //查询购物车内所有商品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findByUserId(BaseContext.getCurrentId());
        //如果购物车存在商品则清空
        if (shoppingCartList != null && shoppingCartList.size() > 0){
            List<Long> ids = new ArrayList<>();
            shoppingCartList.stream().forEach(shoppingCart -> {
                ids.add(shoppingCart.getId());
            });
            shoppingCartMapper.delete(ids);
        }
    }
}
